package com.ruoyi.project.visit.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.visit.domain.DoctorPatientHistory;
import com.ruoyi.project.visit.service.IDoctorPatientHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 历史病例控制器
 */
@RestController
@RequestMapping("/visit/history")
public class DoctorPatientHistoryController extends BaseController {
    @Autowired
    private IDoctorPatientHistoryService doctorPatientHistoryService;

    /**
     * 根据主键查询病例
     * @param historyId
     * @return
     */
    @GetMapping("/{historyId}")
    public AjaxResult getById(@PathVariable Integer historyId){
        return AjaxResult.success(doctorPatientHistoryService.selectByHistoryId(historyId));
    }

    /**
     * 修改病例
     * @param doctorPatientHistory
     * @return
     */
    @PutMapping
    public AjaxResult modify(@RequestBody DoctorPatientHistory doctorPatientHistory){
        // 设置修改病例的用户
        doctorPatientHistory.setUpdateBy(SecurityUtils.getUsername());
        int rows = doctorPatientHistoryService.updateDoctorPatientHistory(doctorPatientHistory);
        if(rows == 0){
            // 修改失败
            return AjaxResult.error();
        }
        // 修改成功
        return AjaxResult.success(doctorPatientHistory);
    }

    /**
     * 新增病例
     * @param doctorPatientHistory
     * @return
     */
    @PostMapping
    public AjaxResult add(@RequestBody DoctorPatientHistory doctorPatientHistory){
        // 设置新增数据的用户
        doctorPatientHistory.setCreateBy(SecurityUtils.getUsername());
        int rows = doctorPatientHistoryService.insertDoctorPatientHistory(doctorPatientHistory);
        if (rows == 0) {
            // 新增病例失败
            return AjaxResult.error();
        }
        // 新增病例成功，把新增后的病例对象返回给前端。前端可以显示病例编号。
        return AjaxResult.success(doctorPatientHistory);
    }

    /**
     * 根据患者主键，分页查询历史病例
     * @param doctorPatientHistory
     * @return
     */
    @GetMapping("/listByPatientId")
    public TableDataInfo listByPatientId(DoctorPatientHistory doctorPatientHistory){
        startPage(); // 分页
        return getDataTable(doctorPatientHistoryService.selectListByPatientId(doctorPatientHistory));
    }
}
